package com.yezong.AidMe.service;

import com.yezong.AidMe.util.ArrayOfString;
import net.sf.json.JSONObject;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

@Service
public class AddressLngLatExchange {

    public String getAddress(String ip){
        JaxWsDynamicClientFactory dcflient=JaxWsDynamicClientFactory.newInstance();

        Client client=dcflient.createClient("http://ws.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx?wsdl");
        try{
            Object[] objects= client.invoke("getCountryCityByIp",ip);
            ArrayOfString as=(ArrayOfString)objects[0];
            for(int i=0;i<objects.length;i++)
                System.out.println("getCountryCityByIp 调用结果："+objects[i]);
            List<String> lstWeatherInfo =as.getString();
            //遍历天气预报信息
            for (String string : lstWeatherInfo) {
                System.out.println(string);
                System.out.println("------------------------");
            }

            return lstWeatherInfo.get(1);

        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    public String getLngLat(String address) {  //根据地址获得经纬度
        StringBuffer json = new StringBuffer();
        try {
            URL u = new URL("http://restapi.amap.com/v3/geocode/geo?address="+address+"&output=JSON&key=3e6378e4d488ce6802e63cfa19e7e16c");
            URLConnection yc = u.openConnection();
            //读取返回的数据
            BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream(),"UTF-8"));
            String inputline = null;
            while((inputline=in.readLine())!=null){
                json.append(inputline);
            }
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        String jsonStr=json.toString();
        JSONObject jsonObject = JSONObject.fromObject(jsonStr);
        //判断输入的位置点是否存在
        if(jsonObject.getJSONArray("geocodes").size()>0)
            return jsonObject.getJSONArray("geocodes").getJSONObject(0).get("location").toString();
        else
            return null;
    }
    public double distanceByLnglat(double _Longitude1, double _Latidute1,
                                   double _Longitude2, double _Latidute2) {
        //根据经纬度计算俩点距离
        double radLat1 = _Latidute1 * Math.PI / 180;
        double radLat2 = _Latidute2 * Math.PI / 180;
        double a = radLat1 - radLat2;
        double b = _Longitude1 * Math.PI / 180 - _Longitude2 * Math.PI / 180;

        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
        s = s * 6378137.0;// 取WGS84标准参考椭球中的地球长半径(单位:m)
        s = Math.round(s * 10000) / 10000;
        return s;
    }

    public double toCalDis(String begin, String end){
        String[] beLngLat=getLngLat(begin).split(",");
        String[] endLngLat=getLngLat(end).split(",");
        System.out.println(beLngLat.toString());
        System.out.println(endLngLat.toString());
        double beLng=Double.parseDouble(beLngLat[0]);
        double beLat=Double.parseDouble(beLngLat[1]);
        double endLng=Double.parseDouble(endLngLat[0]);
        double endLat=Double.parseDouble(endLngLat[1]);

        double res=distanceByLnglat(beLng,beLat,endLng,endLat);

        System.out.println("res:"+res);
        return res;
    }
}
